package com.xiaoyu.merchant.ui.fragment.main;

import android.Manifest;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentTabHost;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;

import com.xiaoyu.commonlib.ui.base.BaseActivity;
import com.xiaoyu.merchant.R;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity implements View.OnClickListener, TabHost.OnTabChangeListener {
    public static FragmentTabHost mTabHost;

    protected static final String TAB_TAG_FOR_COMMODITY = "0";
    protected static final String TAB_TAG_FOR_INFO = "1";
    public static final String TAB_TAG_FOR_UPLOAD = "2";
    protected static final String TAB_TAG_FOR_ORDER = "3";
    protected static final String TAB_TAG_FOR_STORE = "4";

    @BindView(R.id.main_tab_commodity_management)
    LinearLayout mTabCommodity;//商品管理
    @BindView(R.id.main_tab_info)
    LinearLayout mTabInfo;//消息
//    @BindView(R.id.main_tab_upload)
//    LinearLayout mTabUpload;//上传商品
    @BindView(R.id.main_tab_my_order)
    LinearLayout mTabMyOrder;//我的订单
    @BindView(R.id.main_tab_store_management)
    LinearLayout mTabStore;//店铺管理

    @BindView(R.id.main_tab_commodity_management_img)
    ImageView mCommodityImg;
    @BindView(R.id.main_tab_info_img)
    ImageView mInfoImg;
//    @BindView(R.id.main_tab_upload_img)
//    ImageView mUploadImg;
    @BindView(R.id.main_tab_my_order_img)
    ImageView mMyOrderImg;
    @BindView(R.id.main_tab_store_management_img)
    ImageView mStoreImg;

    @BindView(R.id.main_tab_commodity_management_text)
    TextView mCommodityTxt;
    @BindView(R.id.main_tab_info_text)
    TextView mInfoTxt;
//    @BindView(R.id.main_tab_upload_txt)
//    TextView mUploadTxt;
    @BindView(R.id.main_tab_my_order_text)
    TextView mMyOrderTxt;
    @BindView(R.id.main_tab_store_management_text)
    TextView mStoreTxt;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        requestPerssion();
        ButterKnife.bind(this);
        setOccupyViewLinearGradientLogin();
        initView();

    }

    private void initView() {
        mTabCommodity.setOnClickListener(this);
        mTabInfo.setOnClickListener(this);
//        mTabUpload.setOnClickListener(this);
        mTabMyOrder.setOnClickListener(this);
        mTabStore.setOnClickListener(this);

        initTabHost();
    }

    private void initTabHost() {
        mTabHost = this.findViewById(R.id.main_tabhost);
        mTabHost.setup(this, this.getSupportFragmentManager(), R.id.tab_content_layout);
        mTabHost.getTabWidget().setVisibility(View.GONE);
//        mTabHost.getTabWidget().setDividermipmap(null);
        mTabHost.setOnTabChangedListener(this);

        mTabHost.addTab(mTabHost.newTabSpec(TAB_TAG_FOR_COMMODITY).setIndicator("commodity"),
                CommodityFragment.class, null);//商品管理
        mTabHost.addTab(mTabHost.newTabSpec(TAB_TAG_FOR_INFO).setIndicator("info"),
                InfoFragment.class, null);
//        mTabHost.addTab(mTabHost.newTabSpec(TAB_TAG_FOR_UPLOAD).setIndicator("upload"),
//                UploadFragment.class, null);//上传商品
        mTabHost.addTab(mTabHost.newTabSpec(TAB_TAG_FOR_ORDER).setIndicator("order"),
                MyOrderFragment.class, null);//我的订单
        mTabHost.addTab(mTabHost.newTabSpec(TAB_TAG_FOR_STORE).setIndicator("store"),
                StoreFragment.class, null);//商铺管理

    }

    @Override
    public void onClick(View v) {
        if (v == mTabCommodity) {
            mTabHost.setCurrentTabByTag(TAB_TAG_FOR_COMMODITY);
        } else if (v == mTabInfo) {
            mTabHost.setCurrentTabByTag(TAB_TAG_FOR_INFO);
        }
//        else if (v == mTabUpload) {
//            mTabHost.setCurrentTabByTag(TAB_TAG_FOR_UPLOAD);
//        }
        else if (v == mTabMyOrder) {
            mTabHost.setCurrentTabByTag(TAB_TAG_FOR_ORDER);
        } else if (v == mTabStore) {
            mTabHost.setCurrentTabByTag(TAB_TAG_FOR_STORE);
        }
    }

    @Override
    public void onTabChanged(String tabId) {
        if (TAB_TAG_FOR_COMMODITY.equals(tabId)) {
            mCommodityImg.setImageResource(R.mipmap.commodity_blue);
            mInfoImg.setImageResource(R.mipmap.info_gray);
//            mUploadImg.setImageResource(R.mipmap.upload_gray);
            mMyOrderImg.setImageResource(R.mipmap.my_order_gray);
            mStoreImg.setImageResource(R.mipmap.store_gray);

            mCommodityTxt.setTextColor(Color.parseColor("#1772E5"));
            mInfoTxt.setTextColor(Color.parseColor("#A2AAB3"));
//            mUploadTxt.setTextColor(Color.parseColor("#A2AAB3"));
            mMyOrderTxt.setTextColor(Color.parseColor("#A2AAB3"));
            mStoreTxt.setTextColor(Color.parseColor("#A2AAB3"));

        } else if (TAB_TAG_FOR_INFO.equals(tabId)) {
            mCommodityImg.setImageResource(R.mipmap.commodity_gray);
            mInfoImg.setImageResource(R.mipmap.info_blue);
//            mUploadImg.setImageResource(R.mipmap.upload_gray);
            mMyOrderImg.setImageResource(R.mipmap.my_order_gray);
            mStoreImg.setImageResource(R.mipmap.store_gray);

            mCommodityTxt.setTextColor(Color.parseColor("#A2AAB3"));
            mInfoTxt.setTextColor(Color.parseColor("#1772E5"));
//            mUploadTxt.setTextColor(Color.parseColor("#A2AAB3"));
            mMyOrderTxt.setTextColor(Color.parseColor("#A2AAB3"));
            mStoreTxt.setTextColor(Color.parseColor("#A2AAB3"));
        }
//        else if (TAB_TAG_FOR_UPLOAD.equals(tabId)) {
//
//            mCommodityImg.setImageResource(R.mipmap.commodity_gray);
//            mInfoImg.setImageResource(R.mipmap.info_gray);
//            mUploadImg.setImageResource(R.mipmap.upload_blue);
//            mMyOrderImg.setImageResource(R.mipmap.my_order_gray);
//            mStoreImg.setImageResource(R.mipmap.store_gray);
//
//            mCommodityTxt.setTextColor(Color.parseColor("#A2AAB3"));
//            mInfoTxt.setTextColor(Color.parseColor("#A2AAB3"));
//            mUploadTxt.setTextColor(Color.parseColor("#1772E5"));
//            mMyOrderTxt.setTextColor(Color.parseColor("#A2AAB3"));
//            mStoreTxt.setTextColor(Color.parseColor("#A2AAB3"));
//
//        }
        else if (TAB_TAG_FOR_ORDER.equals(tabId)) {
            mCommodityImg.setImageResource(R.mipmap.commodity_gray);
            mInfoImg.setImageResource(R.mipmap.info_gray);
//            mUploadImg.setImageResource(R.mipmap.upload_gray);
            mMyOrderImg.setImageResource(R.mipmap.my_order_blue);
            mStoreImg.setImageResource(R.mipmap.store_gray);

            mCommodityTxt.setTextColor(Color.parseColor("#A2AAB3"));
            mInfoTxt.setTextColor(Color.parseColor("#A2AAB3"));
//            mUploadTxt.setTextColor(Color.parseColor("#A2AAB3"));
            mMyOrderTxt.setTextColor(Color.parseColor("#1772E5"));
            mStoreTxt.setTextColor(Color.parseColor("#A2AAB3"));
        } else if (TAB_TAG_FOR_STORE.equals(tabId)) {

            mCommodityImg.setImageResource(R.mipmap.commodity_gray);
            mInfoImg.setImageResource(R.mipmap.info_gray);
//            mUploadImg.setImageResource(R.mipmap.upload_gray);
            mMyOrderImg.setImageResource(R.mipmap.my_order_gray);
            mStoreImg.setImageResource(R.mipmap.store_blue);

            mCommodityTxt.setTextColor(Color.parseColor("#A2AAB3"));
            mInfoTxt.setTextColor(Color.parseColor("#A2AAB3"));
//            mUploadTxt.setTextColor(Color.parseColor("#A2AAB3"));
            mMyOrderTxt.setTextColor(Color.parseColor("#A2AAB3"));
            mStoreTxt.setTextColor(Color.parseColor("#1772E5"));
        }
    }


    /**
     * Android 6.0权限适配
     */
    private void requestPerssion() {
        if (Build.VERSION.SDK_INT >= 23) {
            String[] mPermissionList = new String[]{
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION};
            ActivityCompat.requestPermissions(this, mPermissionList, 123);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    }

}